package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.ListeningAnswer;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.domain.query.ListeningAnswerQuery;
import com.zhan.ielts.service.ListeningAnswerService;
import com.zhan.ielts.service.UsersService;

@Controller
@RequestMapping("/listening/answer")
public class ListeningAnswerController extends BaseControllerImpl<ListeningAnswer, ListeningAnswerQuery> {

	private Logger log = LoggerFactory.getLogger(ListeningAnswerController.class);

	@Autowired
	private ListeningAnswerService listeningAnswerService;

	@Autowired
	private UsersService usersService;

	@Override
	protected BaseService<ListeningAnswer> getBaseService() {
		return listeningAnswerService;
	}

	/**
	 * @function 跟据userId和时间，获取答案
	 * @param data
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchListeningAnswers(ListeningAnswer data) {
		log.debug("获取答案开始");
		Map map = new HashMap();

		// 检查userId 是否为空
		if (data == null || data.getUserId() == null || data.getUserId().trim().equals("")) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(data.getUserId());
		if (usersService.queryCount(query) < 1) {
			map.put("errCode", 1002);
			return new CustomResult(0, "userId无效！", map);
		}

		try {
			map = listeningAnswerService.fetchListeningAnswers(data);
			log.debug("获取答案结束");
		} catch (Exception e) {
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}

		log.debug("获取答案结束");
		return new CustomResult(1, "", map);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CustomResult updateListeningAnswers(String userId, String answers, String device, String osVersion, String appVersion) {
		log.debug("更新答案开始");
		Map map = new HashMap();

		// 检查userId 是否为空
		if (userId == null || userId.trim().equals("")) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(userId);
		if (usersService.queryCount(query) < 1) {
			map.put("errCode", 1002);
			return new CustomResult(0, "userId无效！", map);
		}

		try {
			JSONArray jan = JSONArray.fromObject(answers);
			List<ListeningAnswer> answerList = JSONArray.toList(jan, ListeningAnswer.class);

			if (answerList != null || answerList.size() != 0) {
				Long updateTime = Long.parseLong(answerList.get(0).getUpdateTime());
				Long currentTime = System.currentTimeMillis() / 1000;
				if (updateTime - currentTime > 3600 * 24 * 2) {

				}

			}

			// 更新答案
			listeningAnswerService.updateListeningAnswers(answerList, userId, device, osVersion, appVersion);
		} catch (Exception e) {
			map.put("errCode", 1002);
			return new CustomResult(0, "系统错误！", map);
		}
		log.debug("更新答案结束");
		return new CustomResult(1, "", "");
	}

}
